package com.lcxw.hxzbapi.controller.roomcontroller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbapi.entity.requestBean.GetRoomByRoomIdReq;
import com.lcxw.hxzbapi.entity.requestBean.GetVideoRoomParkReq;
import com.lcxw.hxzbapi.entity.requestBean.GetVoiceRoomParkReq;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbapi.entity.pojo.RoomRank;
import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import com.lcxw.hxzbapi.entity.pojo.YwpdRoom;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbapi.service.YwpdRoomService;
import com.lcxw.hxzbcommon.common.RedisUtils;
import com.lcxw.hxzbcommon.common.RoomUtils;

@RestController
@RequestMapping("/api/room/range")
@CrossOrigin
@Validated
public class RoomRangeController {

    private Logger logger = LoggerFactory.getLogger(RoomRangeController.class);


    @Resource
    RoomRealization roomRealization;


    @Resource
    RedisUtils redisUtils;

//    @PostMapping("/get/hot")
//    public Response getHotRoomRange(@RequestBody JSONObject requestParams){
//
//        String redisKey = "roomRange:"+requestParams.getString("roomType");
//
//        return getRoomRange(redisKey);
//    }

    @PostMapping("get/general")
    public Response getRoomRange(@RequestBody JSONObject requestParams) {
        String rangeType = requestParams.getString("rangeType");

        if (!"day".equals(rangeType) && !"hour".equals(rangeType) && !"week".equals(rangeType)
            && !"month"
            .equals(rangeType)) {
            return Response.wrongParam(logger, "rangeType");
        }

        String redisKey = "roomEarn:Range:" + rangeType;

        return getRoomRange(redisKey);
    }

    private Response getRoomRange(String redisKey) {
        List list = redisUtils.list_range(redisKey);

        List<RoomRank> roomRankBeans = JSON.parseArray(list.toString(), RoomRank.class);

        Map<String, Object> data = new LinkedHashMap<>();

        data.put("roomRange", roomRankBeans);

        return Response.ok(data);

    }

    /**
     * 获取房间广场
     */
    @PostMapping("get/roomPark")
    public Response getRoomPark(@RequestBody JSONObject requestParams) {

        String roomType = requestParams.getString("roomType");

        if (!"voice".equals(roomType) && !"video".equals(roomType) && !"fullVideo".equals(roomType)
            && !"paipai".equals(roomType)) {
            return Response.wrongParam(logger, "roomType");
        }
        Map<String, Object> data = new LinkedHashMap<>();

        List roomRange = roomRealization.getRoomPark(roomType);

        data.put("roomRange", roomRange);

        return Response.ok(data);
    }

    /**
     * 获取语音聊天室广场
     */
    @PostMapping("/get/voice/roomPark")
    public Response getVoiceRoomPark(@RequestBody GetVoiceRoomParkReq getVoiceRoomParkReq) {
        int pageNum = getVoiceRoomParkReq.getPageNum();
        int pageSize = getVoiceRoomParkReq.getPageSize();
        List voiceRoomPark = roomRealization.getVoiceRoomPark(pageNum, pageSize);
        return Response.ok(voiceRoomPark);
    }

    /**
     * 获取我关注的正在直播的聊天室
     */
    @PostMapping("/get/voice/roomLivePark")
    public Response roomLivePark(@RequestBody GetVoiceRoomParkReq getVoiceRoomParkReq) {
        List voiceRoomPark = roomRealization.getVoiceRoomLivePark(getVoiceRoomParkReq);
        return Response.ok(voiceRoomPark);
    }
    /**
     * 获取直播房间广场
     */
    @PostMapping("/get/video/roomPark")
    public Response getVideoRoomPark(@RequestBody GetVideoRoomParkReq getVideoRoomParkReq) {
        int pageNum = getVideoRoomParkReq.getPageNum();
        int pageSize = getVideoRoomParkReq.getPageSize();
        String liveType = getVideoRoomParkReq.getLiveType();
        List videoRoomPark = roomRealization.getVideoRoomPark(liveType, pageNum, pageSize);
        JSONObject data = new JSONObject();
        data.put("roomRange", videoRoomPark);
        return Response.ok(data);
    }


    /**
     * 根据房间号查找在播房间
     */
    @PostMapping("/get/byRoomId")
    public Response getRoomByRoomId(@RequestBody GetRoomByRoomIdReq getRoomByRoomIdReq) {
        long roomId = getRoomByRoomIdReq.getRoomId();
        return roomRealization.getLivingRoomByRoomId(roomId);
    }


}
